package com.yixin.hospital.service.impl;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.UserIpWhiteList;
import com.yixin.hospital.entity.dto.req.userIpWhiteList.UserIpWhiteListAddReqDTO;
import com.yixin.hospital.entity.dto.req.userIpWhiteList.UserIpWhiteListEditReqDTO;
import com.yixin.hospital.service.UserIpWhiteListService;
import com.yixin.hospital.mapper.UserIpWhiteListMapper;
import com.yixin.hospital.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
* @author 
* @description 针对表【user_ip_white_list(用户ip白名单表)】的数据库操作Service实现
* @createDate 2024-03-26 21:02:35
*/
@Service
public class UserIpWhiteListServiceImpl extends ServiceImpl<UserIpWhiteListMapper, UserIpWhiteList>
    implements UserIpWhiteListService{

    @Override
    public ApiResult add(UserIpWhiteListAddReqDTO userIpWhiteListAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        UserIpWhiteList userIpWhiteList = new UserIpWhiteList();
        userIpWhiteList.setIp(ServletUtil.getClientIP(request));
        userIpWhiteList.setUserid(userIpWhiteListAddReqDTO.getUserId());
        userIpWhiteList.setCreatedBy(userName);
        userIpWhiteList.setUpdatedBy(userName);
        this.save(userIpWhiteList);
        return ApiResult.success();
    }

    @Override
    public ApiResult edit(UserIpWhiteListEditReqDTO userIpWhiteListEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        UserIpWhiteList userIpWhiteList = new UserIpWhiteList();
        userIpWhiteList.setId(userIpWhiteListEditReqDTO.getId());
        userIpWhiteList.setIp(ServletUtil.getClientIP(request));
        userIpWhiteList.setUserid(userIpWhiteListEditReqDTO.getUserId());
        userIpWhiteList.setUpdatedBy(userName);
        this.save(userIpWhiteList);
        return ApiResult.success();
    }
}




